﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BLL;
using Library;

/// <summary>
/// Summary description for WSConfig
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class WSConfig : System.Web.Services.WebService
{

    #region Admin
    [WebMethod]
    public int AddAdmin(string userName,string pass, string fullName, string birthday, string YM, string phone, string description, short status)
    {
        CtrAdmin ctrAdmin = new CtrAdmin();
        int result = ctrAdmin.InsertAdmin(userName,pass, fullName, birthday, YM, phone, description, status);
        if (result > 0)
        {
            string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
            string des = "Thêm mới Admin có mã [AdminID: " + result + "] với các thông tin: [userName: " + userName + ", fullname: " + fullName + ", status: " + status + ", birthday: " + birthday + ", YM: " + YM + ", telephone: " + phone + ", description: " + description + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Add, des, function);
        }
        return result;
    }

    [WebMethod]
    public int DeleteAdmin(int adminID)
    {
        CtrAdmin ctrAdmin = new CtrAdmin();
        int result = ctrAdmin.DeleteAdmin(adminID);
        if (result > 0)
        {
            string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
            string des = "Xóa Admin có mã [AdminID: " + adminID + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Delete, des, function);
        }
        return result;
    }

    [WebMethod]
    public int UpdateAdminInfo(string fullName, string birthday, string YM, string phone)
    {
        CtrAdmin ctrAdmin = new CtrAdmin();
        DateTime birthdayDate = (DateTime)Lib.GetDateTime(birthday);
        int result = ctrAdmin.UpdateAdminInfo(fullName, birthdayDate, YM, phone);
        if (result > 0)
        {
            string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
            string des = "Cập nhật thông tin cá nhân sang: [fullname: " + fullName + ", birthday: " + birthday + ", YM: " + YM + ", telephone: " + phone + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Modify, des, function);
        }
        return result;
    }

    [WebMethod]
    public int UpdateAdminStatus(int adminID, string description, short status)
    {
        CtrAdmin ctrAdmin = new CtrAdmin();
        int result = ctrAdmin.UpdateAdminStatus(adminID, description, status);
        if (result > 0)
        {
            string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
            string des = "Cập nhật trạng thái và mô tả Admin có mã: [adminID:" + adminID + "] sang: [status: " + ((status == 1) ? "Kích hoạt" : "Khóa") + ", description: " + description + "]";
            CtrLog.InsertLog(Config.LogActionConstants.ChangeStatus, des, function);
        }
        return result;
    }

    [WebMethod]
    public int ChangePass(string passOld, string passNew)
    {
        CtrAdmin ctrAdmin = new CtrAdmin();
        int result = ctrAdmin.ChangePass(passOld,passNew);
        if (result > 0)
        {
            string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
            string des = "Đổi mật khẩu";
            CtrLog.InsertLog(Config.LogActionConstants.ChangeStatus, des, function);
        }
        return result;
    }
    #endregion

    #region Function
    [WebMethod]
    public int AddMenu(string name, int parrentID, string url, int status)
    {
        CtrFunction ctrFunction = new CtrFunction();
        int result = ctrFunction.AddFunction(name, parrentID, url, status);
        if (result > 0)
        {
            string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
            string des = "Thêm mới chức năng có mã: [FunctionID: " + result + "] với các thông tin: [name: " + name + ", parrentID: " + parrentID + ", url: " + url + ", status: " + status + "]"; ;
            CtrLog.InsertLog(Config.LogActionConstants.Add, des, function);
        }
        return result;
    }

    [WebMethod]
    public int UpdateMenu(int menuID, string name, int parrentID, string url, int order, int status)
    {
        CtrFunction ctrFunction = new CtrFunction();
        int result = ctrFunction.UpdateFunction(menuID, name, parrentID, url, order, status);
        if (result > 0)
        {
            string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
            string des = "Cập nhật chức năng có mã: [FunctionID: " + menuID + "] thành: [name: " + name + ", parrentID: " + parrentID + ", url: " + url + ", order: " + order + ", status: " + status + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Modify, des, function);
        }
        return result;
    }

    [WebMethod]
    public int DeleteMenu(int menuID)
    {
        CtrFunction ctrFunction = new CtrFunction();
        int result = ctrFunction.DeleteFunction(menuID);
        if (result > 0)
        {
            string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
            string des = "Xóa chức năng có mã: [functionID: " + menuID + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Delete, des, function);
        }
        return result;
    }
    #endregion

    #region role
    [WebMethod]
    public int UpdateRoleInfo(int roleID, string name, string description, short status)
    {
        CtrRole ctrRole = new CtrRole();
        int result = ctrRole.UpdateRole(roleID, name, description, status);
        if (result > 0)
        {
            string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
            string des = "Cập nhật thông tin nhóm quyền có mã: [roleID: " + roleID + "] thành: [name: " + name + ", description: " + description + ", status: " + status + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Modify, des, function);
        }
        return result;
    }

    [WebMethod]
    public int AddRole(string name, string description, short status)
    {
        CtrRole ctrRole = new CtrRole();
        int result = ctrRole.InsertRole(name, description, status);
        if (result > 0)
        {
            string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
            string des = "Thêm mới nhóm quyền có mã: [roleID: " + result + "] với các thông tin: [name: " + name + ", description: " + description + ", status: " + status + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Add, des, function);
        }
        return result;
    }

    [WebMethod]
    public int DeleteRole(int roleID)
    {
        CtrRole ctrRole = new CtrRole();
        int result = ctrRole.DeleteRole(roleID);
        if (result > 0)
        {
            string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
            string des = "Xóa nhóm quyền có mã: [roleID: " + roleID + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Delete, des, function);
        }
        return result;
    }
    #endregion

    #region rolefunction
    [WebMethod]
    public int AddRoleFunction(int roleID, int menuID)
    {
        CtrRoleFunction ctrRolefunction = new CtrRoleFunction();
        int result = ctrRolefunction.InsertRoleFunction(roleID, menuID);
        if (result > 0)
        {
            string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
            string des = "Thêm chức năng có mã: [menuID: " + menuID + "] vào nhóm quyền có mã: [roleID: " + roleID + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Add, des, function);
        }
        return result;
    }

    [WebMethod]
    public int DeleteRoleFunction(int roleID, int menuID)
    {
        CtrRoleFunction ctrRolefunction = new CtrRoleFunction();
        int result = ctrRolefunction.DeleteRoleFunction(roleID, menuID);
        if (result > 0)
        {
            string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
            string des = "Xóa chức năng có mã: [menuID: " + menuID + "] khỏi nhóm quyền có mã: [roleID: " + roleID + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Delete, des, function);
        }
        return result;
    }

    [WebMethod]
    public int DeleteRoleFunctionMulti(int roleID, string menuIDs)
    {
        CtrRoleFunction ctrRolefunction = new CtrRoleFunction();
        int result = ctrRolefunction.DeleteRoleFunctionMulti(roleID, menuIDs);
        if (true)
        {
            string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
            string des = "Xóa các chức năng có mã: [functionIDs:" + menuIDs + "] khỏi nhóm quyền có mã: [roleID: " + roleID + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Modify, des, function);
        }
        return result;
    }

    [WebMethod]
    public string AddRoleFunctionMulti(int roleID, string menuIDs)
    {
        CtrRoleFunction ctrRolefunction = new CtrRoleFunction();
        string result = ctrRolefunction.InsertRoleFunctionMulti(roleID, menuIDs);
        if (true)
        {
            string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
            string des = "Thêm các chức năng có mã: [functionIDs" + menuIDs + "] vào nhóm quyền có mã: [roleID: " + roleID + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Modify, des, function);
        }
        return result;
    }

    [WebMethod]
    public int UpdatePermissionRoleFunction(int roleID, int menuID, int permission)
    {
        CtrRole ctrRole = new CtrRole();
        int result = ctrRole.UpdateRoleFunction(roleID, menuID, permission);
        return result;
    }
    #endregion

    #region permission
    [WebMethod]
    public int AddAdminToRole(int roleID, int adminID)
    {
        CtrPermission ctrPermission = new CtrPermission();
        int result = ctrPermission.InsertPermission(adminID, roleID);
        if (result > 0)
        {
            string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);
            string des = "Thêm admin có mã: [adminID: " + adminID + "] vào nhóm quyền có mã: [roleID: " + roleID + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Modify, des, function);
        }
        return result;
    }

    [WebMethod]
    public int DeletePermission(int permisionID)
    {
        CtrPermission ctrPermission = new CtrPermission();
        int result = ctrPermission.DeletePermission(permisionID);
        if (result > 0)
        {
            string function = HttpContext.Current.Request.Headers["Referer"].Substring(HttpContext.Current.Request.Headers["Referer"].LastIndexOf("/") + 1);

            string des = "Xóa permission (phân quyền role cho admin) có mã: [permissionID: " + permisionID + "]";
            CtrLog.InsertLog(Config.LogActionConstants.Modify, des, function);
        }
        return result;
    }

    #endregion
}
